package tk.deep.data.csv;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.SequenceWriter;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import lombok.SneakyThrows;
import tk.deep.data.DataBean;
import tk.deep.data.DataBeanX;
import tk.deep.data.DataBeanXX;
import tk.deep.data.DataBeanXXFormat;

import java.io.File;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * jackson 读写csv.
 *
 * 更多信息请访问：
 * https://github.com/FasterXML/jackson-dataformats-text/tree/master/csv
 * @author Asin Liu
 * @version  1.0
 */
public class CSVMain{
    public static void main(String[] args) {
        List<DataBean> lst = new ArrayList<>();
        DataBean db  = new DataBean(Long.MAX_VALUE,"xksdidk",Float.MAX_VALUE,Double.MAX_VALUE,Byte.MAX_VALUE,Short.MAX_VALUE,Integer.MAX_VALUE,'中', BigDecimal.TEN,new Date());
        DataBean db2  = new DataBean(Long.MIN_VALUE,"xksdidk",Float.MIN_VALUE,Double.MIN_VALUE,Byte.MIN_VALUE,Short.MIN_VALUE,Integer.MIN_VALUE,'国', BigDecimal.TEN,new Date());
        lst.add(db);
        lst.add(db2);

        String txt = toCSV(lst);
        System.out.println(txt);


        List<DataBeanX> list = new ArrayList<>();
        DataBeanX dbx = new DataBeanX("aa",13,BigDecimal.TEN,new Date());
        DataBeanX dbx2 = new DataBeanX("bb",31,BigDecimal.TEN,new Date());
        list.add(dbx);
        list.add(dbx2) ;
        System.out.println(toCSVWithOrder(list));

        System.out.println("=====================");

        List<DataBeanXX> lstx = new ArrayList<>();
        DataBeanXX dbxx = new DataBeanXX("cc",13,BigDecimal.ONE,new Date());
        DataBeanXX dbxx2 = new DataBeanXX("dd",31,BigDecimal.TEN,new Date());
        lstx.add(dbxx);
        lstx.add(dbxx2) ;
        System.out.println(toCSVWithOrderWithLB(lstx));

        // 写入到文件中
        CsvMapper mapper = new CsvMapper();
        // Note 同时使用两个格式化和一个JsonIgnore，看看输出效果噢
        mapper.addMixIn(DataBeanXX.class, DataBeanXXFormat.class);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        CsvSchema schema = mapper.schemaFor(DataBeanXX.class).withHeader();
        try (SequenceWriter writer = mapper.writer(schema).writeValues(new File("data.csv"))) {
            writer.write(lstx);
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 读取文件
        CsvSchema schema1 = CsvSchema.emptySchema().withHeader();
        System.out.println("read from file");
        try (InputStream inputStream = CsvSchema.class.getClassLoader().getResourceAsStream("data.csv")) {
            MappingIterator<DataBeanXX> values =  mapper.readerFor(DataBeanXX.class).with(schema1).readValues(inputStream);
            System.out.println(">>> values:" );
            values.forEachRemaining(System.out::println);
        } catch (Exception e) {
            e.printStackTrace();
        }



    }


    @SneakyThrows(JsonProcessingException.class)
    public static String toCSV (List<DataBean> listOfPojos)  {
        CsvMapper mapper = new CsvMapper();
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        CsvSchema schema = mapper.schemaFor(DataBean.class).withHeader();
        return mapper.writer(schema).writeValueAsString(listOfPojos);
    }


    @SneakyThrows(JsonProcessingException.class)
    public static String toCSVWithOrder (List<DataBeanX> listOfPojos)  {
        CsvMapper mapper = new CsvMapper();
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        CsvSchema schema = mapper.schemaFor(DataBeanX.class).withHeader();
        return mapper.writer(schema).writeValueAsString(listOfPojos);
    }


    @SneakyThrows(JsonProcessingException.class)
    public static String toCSVWithOrderWithLB (List<DataBeanXX> listOfPojos)  {
        CsvMapper mapper = new CsvMapper();
        // Note 同时使用两个格式化和一个JsonIgnore，看看输出效果噢
        mapper.addMixIn(DataBeanXX.class, DataBeanXXFormat.class);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        CsvSchema schema = mapper.schemaFor(DataBeanXX.class).withHeader();
        return mapper.writer(schema).writeValueAsString(listOfPojos);
    }

}
